#include "windows.h"
#include "widget.h"
#include "./ui_widget.h"
#include <QDebug>
#include <QShortcut>
#include <QFileDialog>
#include <QSettings>

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 添加快捷键
    addHotKey();
}

Widget::~Widget()
{
    delete ui;
}

// 添加快捷键
void Widget::addHotKey()
{
    // 当使用快捷键ctrl+D时，进行更新坐标
    QShortcut *shortCut = new QShortcut(QKeySequence("Ctrl+D"), this);
    connect(shortCut, &QShortcut::activated, this, &Widget::updateCoordinateText);
}

// 更新坐标
void Widget::updateCoordinateText()
{
    POINT point;
    GetCursorPos(&point);
    QString coordinateXText = QString::number(point.x);
    QString coordinateYText = QString::number(point.y);
    ui->coordinate_x->setText(coordinateXText);
    ui->coordinate_y->setText(coordinateYText);
}

void Widget::on_pushButton_clicked()
{

    qDebug() << "Hello World!";
}

void Widget::on_add_script_clicked()
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(rowCount + 1);

    // 添加表格序号列
    setTableWidgetCell(rowCount, 0, QString::number(rowCount + 1));

    //　添加表格鼠标列
    QString clickText;
    if (ui->single_click->isChecked()) {

        clickText = ui->single_click->text();
    } else if(ui->double_click->isChecked()) {

        clickText = ui->double_click->text();
    } else {
        clickText = QString("无");
    }
    setTableWidgetCell(rowCount, 1, clickText);


    //　添加表格按键列
    QString pressKeyText;
    if (ui->left_button->isChecked()) {

        pressKeyText = ui->left_button->text();
    } else if(ui->right_button->isChecked()) {

        pressKeyText = ui->right_button->text();
    } else {
        pressKeyText = QString("无");
    }
    setTableWidgetCell(rowCount, 2, pressKeyText);

    // 添加表格坐标列
    QString coordinateXText = ui->coordinate_x->text();
    QString coordinateYText = ui->coordinate_y->text();
    setTableWidgetCell(rowCount, 3, coordinateXText + "," + coordinateYText);

    // 添加表格延时列
    QString delayText = ui->delay_seconds->text();
    setTableWidgetCell(rowCount, 4, delayText);
}

// 设置表格内容
void Widget::setTableWidgetCell(int rowNumber, int columnNumber, QString text)
{
    QTableWidgetItem *item = new QTableWidgetItem();
    item->setText(text);
    item->setTextAlignment(Qt::AlignCenter);
    ui->tableWidget->setItem(rowNumber, columnNumber, item);
}

// 删除选中行
void Widget::on_delete_script_clicked()
{
    QList<QTableWidgetItem*> q = ui->tableWidget->selectedItems();

    if (q.isEmpty()) return;
    int rowNumber = q.first()->row();
    ui->tableWidget->removeRow(rowNumber);
}

// 保存脚本
void Widget::on_save_script_clicked()
{
    QFileDialog fileLog;
    QString filePath = fileLog.getSaveFileName(this, QString("保存"), "./", QString("ini File(*.ini)"));
    if (filePath.length() == 0) {

        qDebug() << "文件没有选择";
        return ;
    }
    QSettings *settingFile = new QSettings(filePath, QSettings::IniFormat);
    // 查看是否有坐标点位置
    if (ui->tableWidget->rowCount() == 0) {

        qDebug() << "列表中没有数据";
        settingFile->sync();
        delete settingFile;
        return;
    }
    settingFile->clear();
    // 有数据处理
    for (int r=0; r < ui->tableWidget->rowCount(); r++) {

        settingFile->beginGroup(QString::number(r));
        for (int c=0; c < ui->tableWidget->columnCount(); c++) {

            QTableWidgetItem *item = ui->tableWidget->item(r, c);
            switch (c) {
            case 0:
                // 序号
                {}
                break;
            case 1:
                // 鼠标
            {

                QString::compare(item->text(), "单击") == 0
                    ? settingFile->setValue(QString("mouse"), QString("single"))
                    : settingFile->setValue(QString("mouse"), QString("double"));
            }
                break;
            case 2:
                // 按键
            {
                QString::compare(item->text(), "左键") == 0
                    ? settingFile->setValue(QString("press_key"), QString("left"))
                    : settingFile->setValue(QString("press_key"), QString("right"));
            }
                break;
            case 3:
                // 坐标
            {
                QStringList strList = item->text().split(',');
                settingFile->setValue(QString("x"), strList[0]);
                settingFile->setValue(QString("y"), strList[1]);
            }
                break;
            case 4:
                // 延时
            {
                settingFile->setValue(QString("delay"), item->text());
            }
                break;
            default:
            {}
                break;
            }
        }
        settingFile->endGroup();
    }
    settingFile->sync();
    delete settingFile;
}

// 导入脚本
void Widget::on_import_script_clicked()
{
    QFileDialog fileLog;
    QString filePath = fileLog.getOpenFileName(this, QString("保存"), "./", QString("ini File(*.ini)"));
    if (filePath.length() == 0) {

        qDebug() << "文件没有选择";
        return ;
    }
    QSettings *settingFile = new QSettings(filePath, QSettings::IniFormat);
    QStringList strList = settingFile->childGroups();
    if (strList.length() == 0) {

        delete settingFile;
        return;
    }
    ui->tableWidget->setRowCount(0);
    for (int offset = 0; offset < strList.length(); offset++) {
        settingFile->beginGroup(strList[offset]);
        QStringList allKeys = settingFile->childKeys();
        int rowCount = ui->tableWidget->rowCount() + 1;
        ui->tableWidget->setRowCount(rowCount);
        if (allKeys.length() != 0) {

            QTableWidgetItem *rowItem = new QTableWidgetItem(QString::number(rowCount));
            rowItem->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(offset, 0, rowItem);
            if (allKeys.contains("mouse")) {

                QString s = settingFile->value(QString("mouse")).toString();
                QString itemString = s.compare(QString("signal")) == 0
                        ? QString("单击") : QString("双击");
                QTableWidgetItem *item = new QTableWidgetItem(itemString);
                item->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(offset, 1, item);
            }
            if (allKeys.contains("press_key")) {

                QString s = settingFile->value(QString("press_key")).toString();
                QString itemString = s.compare(QString("left")) == 0
                        ? QString("左键") : QString("右键");
                QTableWidgetItem *item = new QTableWidgetItem(itemString);
                item->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(offset, 2, item);
            }

            if (allKeys.contains("x") && allKeys.contains("y")) {

                QString sx = settingFile->value(QString("x")).toString();
                QString sy = settingFile->value(QString("y")).toString();
                QTableWidgetItem *item = new QTableWidgetItem(sx + QString(",") + sy);
                item->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(offset, 3, item);
            }
            if (allKeys.contains("delay")) {

                QString s = settingFile->value(QString("delay")).toString();
                QTableWidgetItem *item = new QTableWidgetItem(s);
                item->setTextAlignment(Qt::AlignCenter);
                ui->tableWidget->setItem(offset, 4, item);
            }
        }
        settingFile->endGroup();
    }
    delete settingFile;
}

// 运行脚本
void Widget::on_run_script_clicked()
{

}
